﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web.Script.Serialization;
using Codeplex.Data;

namespace Bot_Schedule.Lib
{
    public static class Functions
    {
        //WEB
        public static string getdata(WebClient wc, string input, string value)
        {
            string wc_return = null;
            value = EncodeTo64(value);
            try
            {
                byte[] bret = wc.DownloadData("http://dangkhoasoft.com/seo/base/index/" + input + "/" + value);
                //byte[] bret = wc.DownloadData("http://localhost/dkservice/base/index/" + input + "/" + value);
                wc_return = Encoding.UTF8.GetString(bret);
                wc_return = string.IsNullOrEmpty(wc_return) ? null : wc_return;
            }
            catch (System.Exception ex)
            {
                wc_return = null;
            }
            return wc_return;
        }
        public static string getdataReq(WebClient wc, string input, string value)
        {
            string wc_return = null;
            try
            {
                NameValueCollection inputs = new NameValueCollection();
                inputs.Add(input, value);
                byte[] bret = wc.UploadValues("http://dangkhoasoft.com/seo/base/index/" + input + "/", inputs);

                wc_return = Encoding.UTF8.GetString(bret);
                wc_return = string.IsNullOrEmpty(wc_return) ? null : wc_return;
            }
            catch (System.Exception ex)
            {
                wc_return = null;
            }
            return wc_return;
        }
        public static dynamic convertData(WebClient wc, string input, string value)
        {
            dynamic result = null;
            try
            {
                string json = getdata(new WebClient(), input, value);
                result = DynamicJson.Parse(json);
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }
        public static DataTable GetDataTableFromObjects(object[] objects)
        {
            if (objects != null && objects.Length > 0)
            {
                Type t = objects[0].GetType();
                DataTable dt = new DataTable(t.Name);
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    dt.Columns.Add(new DataColumn(pi.Name));
                }
                foreach (var o in objects)
                {
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn dc in dt.Columns)
                    {
                        dr[dc.ColumnName] = o.GetType().GetProperty(dc.ColumnName).GetValue(o, null);
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }
            return null;
        }
        public static DataTable ToDataTable(List<Dictionary<string, string>> list)
        {
            DataTable result = new DataTable();
            if (list.Count == 0)
                return result;

            var columnNames = list.SelectMany(dict => dict.Keys).Distinct();
            result.Columns.AddRange(columnNames.Select(c => new DataColumn(c)).ToArray());
            foreach (Dictionary<string, string> item in list)
            {
                var row = result.NewRow();
                foreach (var key in item.Keys)
                {
                    row[key] = item[key];
                }

                result.Rows.Add(row);
            }

            return result;
        }
        public static string EncodeTo64(string toEncode)
        {
            byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(toEncode);
            string enc = Convert.ToBase64String(encbuff);
            return System.Web.HttpUtility.UrlEncode(enc);
        }
    }
}
